package main;

import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import entity.Pubmed;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import utils.PubmedUtils2;
import utils.SmallTool;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 葛成瑞
 * @crete 2021/2/27
 */
public class ReadDb {

    static String dbName = "xml_content";
    static String insetTable = "pubmed_vip_extend_new_new";
//    static String insetanaTable = "pubmed_analysis";


    @SneakyThrows
    public static void main(String[] args) {

        //泰医服务器解析报错了 还没看原因 2021年3月27日09:08:45  在29668557 下面那个pmid volume 超长报错了

        String allDate = "select max(pmid) as pmid  from " + dbName;
        Entity query = Db.use().query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("pmid"));

        Integer count = query.getInt("pmid");
        int breakSize = 100000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
//        SmallTool.printMess("即将开始请提前关闭如果想的话 倒计时十秒");
//        Thread.sleep(10000);

        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        //shuju
        for (int i = 1; i <= pageSize; i++) {
            SmallTool.printMess("当前页码+++++" + i);
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            String paramSql = "select * from " + dbName + " where  pmid between  " + start + " and " + end;
            List<Entity> querys = Db.use().query(paramSql);
            querys.parallelStream().forEach(entity -> {
                String content = ZipUtil.unGzip(entity.getBytes("content"), "utf-8");
//                //解析html
                Document doc = Jsoup.parse(content);
                try {
                    // 主要是用于进行过滤掉参文房子参文doi混入文章doi
                    doc.select("ReferenceList").remove();
                } catch (Exception e) {

                }
                List<Element> divList = new ArrayList<>();
                Elements pubmedArticles = doc.select("PubmedArticle");
                Elements pubmedBookArticles = doc.select("PubmedBookArticle");
                for (Element e : pubmedArticles) {
                    divList.add(e);
                }
                for (Element e : pubmedBookArticles) {
                    divList.add(e);
                }
                //
                List<Pubmed> pubmeds = PubmedUtils2.parseHtml(divList);
                try {

                    insertData(pubmeds.get(0));
                } catch (Exception e) {

                }
                //直接入库
//                try {
//                    for (Pubmed pubmed : pubmeds) {
//                        insertData(pubmed);
//                    }
//                }catch (Exception e){
//
//                }

            });
//
//            for (Entity entity : querys) {
//
//
//            }
        }

    }

    @SneakyThrows
    public static void insertData(Pubmed pubmed) {

        Db.use().insert(new Entity().setTableName(insetTable)
                .set("pmid", pubmed.getPmid())
                .set("type", pubmed.getType())
                .set("article_title", pubmed.getArticleTitle())
                .set("source", pubmed.getSource())
                .set("intro", pubmed.getIntro())
                .set("jour", pubmed.getJour())
                .set("volume", pubmed.getVolume())
                .set("issue", pubmed.getIssue())
                .set("pages", pubmed.getPages())
                .set("vip", pubmed.getVip())
                .set("pub_date", pubmed.getPubDate())
                .set("medline_date", pubmed.getMedlineDate())
                .set("pub_year", pubmed.getPubYear())
                .set("med_nlm_id", pubmed.getMedNlmId())
                .set("author", pubmed.getAuthor())
                .set("author_extend", pubmed.getAuthorExtend())
                .set("affs", pubmed.getAffs())
                .set("pmcid", pubmed.getPmcid())
                .set("doi", pubmed.getDoi())
                .set("lan", pubmed.getLan())
                .set("ab_text", pubmed.getAbText())
                .set("copyright", pubmed.getCopyright())
                .set("keyword", pubmed.getKeyword())
                .set("pts", pubmed.getPts())
                .set("meshs", pubmed.getMeshs())
                .set("mesh_heading", pubmed.getMeshHeading())
                .set("mesh_headingy", pubmed.getMeshHeadingy())
                .set("subs", pubmed.getSubs())
                .set("grants", pubmed.getGrants())
                .set("ass_date", pubmed.getAssDate())
                .set("full_text", pubmed.getFullText())
                .set("art_type", pubmed.getArtType())
        );

    }
}
